//解析 entry[name]符号
function parseField(field) {
    return field
        .split(/\[|\]/)
        .filter((s) => s);
}
//基于 parseField()的结果查找属性
function getField(req, field) {
    let val = req.body;
    console.log(`getField ${field}`)        // field = entry,title
    field.forEach((prop) => {
        //循环第1次，req.body['entry']; 循环第2次, req.body['entry']['title']
        val = val[prop];
        console.log(val)
    });
    return val;
}
exports.required = (field) => {
    console.log(`required--验证 ${field}`);
    console.log("required--执行parseField");    
    field = parseField(field);
    console.log(`required--parseField结果 ${field}`);
    return (req, res, next) => {
        console.log(`开始验证 ${field}`)
        if (getField(req, field)) {
            next();
        } else {
            console.log(`${field.join(' ')} is required`)
            res.error(`${field.join(' ')} is required`);
            res.redirect('back');
        }
    };
};
exports.lengthAbove = (field, len) => {
    field = parseField(field);
    return (req, res, next) => {
        if (getField(req, field).length > len) {
            next();
        } else {
            const fields = field.join(' ');
            res.error(`${fields} must have more than ${len} characters`);
            res.redirect('back');
        }
    };
};